<template>
    <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <h2 class="sub-header">添加信息</h2>
    <div class="form-group" id="radio">
      <h3>姓名</h3>
      <br>
      <input type="radio" name="name" v-model="student.name" id="fx"  value="冯希">
      <label for="fx">冯希</label><br>
      <input type="radio" name="name" v-model="student.name" id="yx" value="杨鑫">
      <label for="yx">杨鑫</label><br>
      <input type="radio" name="name" v-model="student.name" id="cxp" value="陈细盼">
      <label for="cxp">陈细盼</label><br>
      <input type="radio" name="name" v-model="student.name" id="dl" value="杜拉">
      <label for="dl">杜拉</label><br>
    </div>
    <div class="form-group">
     <h3>模板名称</h3>
      <input
        class="form-control"
        type="text"
        v-model="student.hobbies"
        id
        name="hobbies"
        placeholder="请输入模板名称"
      >
    </div>
    <div class="form-group">
     <h3>域名</h3>
      <input
        class="form-control"
        type="text"
        v-model="student.www"
        id
        name="www"
        placeholder="请输入域名"
      >
    </div>
    <router-link
      @click.native="doSubmit"
      to="/students"
      class="btn btn-primary"
    >提交</router-link>
    <router-link class="btn btn-danger" to="/students">取消</router-link>
  </div>
</template>
<script>
import { Toast } from 'mint-ui';
export default {
  data() {
    return {
      student:{},
      id:0
    };
  },
  methods: {
    doSubmit(e) {

      if (!this.student.name) {
        Toast({
          message: "姓名不能为空！",
          position: "middle",
          duration: 2000,
          iconClass: 'glyphicon glyphicon-remove'
        });
        return false;
      }
      if (!this.student.hobbies) {
        Toast({
          message: "模板名称不能为空！",
          position: "middle",
          duration: 2000,
          iconClass: 'glyphicon glyphicon-remove'
        });
        return false;
      }else {
        var formData = JSON.stringify(this.student);
        this.$http
          .post("/students/edit", formData)
          .then(response => {
            this.$parent.getStudents();
          })
          .catch(function() {
            console.log("服务器异常！");
          });
        Toast({
          message: "修改信息成功！",
          position: "middle",
          duration: 2000,
          iconClass: 'glyphicon glyphicon-ok'
        });
        this.student.hobbies = ""
      }
    },
    getStudent(){
      var id = this.id
      this.$http
          .get("/students/edit", {params:{id}})
          .then(response => {
            this.student = response.body
          })
          .catch(function() {
            console.log("服务器异常！");
          });
    }

  },
  created: function() {
    this.id = this.$route.params.id
    this.getStudent()
  },
};
</script>
